package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.enums.DubboServiceEnum;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.common.util.ChangeDubboUtil;
import cc.rengu.igas.bsps.core.model.UserSessionInfo;
import cc.rengu.igas.bsps.core.realize.UserPermissionCheckService;
import cc.rengu.igas.bsps.core.realize.impl.UserPermissionCheckServiceImpl;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.momp.facade.request.ManageCashierAccountRequest;
import cc.rengu.igas.momp.facade.response.ManageCashierAccountResponse;
import cc.rengu.igas.momp.facade.result.Result;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;

/**
 * @author Xuegui Cao
 * @date 2020/7/1
 * @description 收银员收款账号配置
 */
public class ManageCashierAccountService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        ManageCashierAccountRequest request = new ManageCashierAccountRequest();
        // 读取存在xml树里的实体类
        ConvertUtil.convertOutput(request);

        String session = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SESSION);
        UserPermissionCheckService userService = new UserPermissionCheckServiceImpl();
        UserSessionInfo userSessionInfo = userService.checkSessionStatus(request.getHeader().getInstId(), session);
        String sensitiveKey = userSessionInfo.getSensitiveKey();

        String acctNo = request.getAcctInfo().getAcctNo();
        acctNo = descriptData(sensitiveKey, acctNo);
        request.getAcctInfo().setAcctNo(acctNo);
        String idNo = request.getAcctInfo().getIdNo();
        idNo = descriptData(sensitiveKey, idNo);
        request.getAcctInfo().setIdNo(idNo);
        String cellPhoneNumber = request.getAcctInfo().getCellPhoneNumber();
        cellPhoneNumber = descriptData(sensitiveKey, cellPhoneNumber);
        request.getAcctInfo().setCellPhoneNumber(cellPhoneNumber);

        ConvertUtil.convertInput(request);
        return request;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        ManageCashierAccountRequest dubboRequest = (ManageCashierAccountRequest) request;
        /*校验用户商户绑定关系,商户所属机构*/
        ChangeDubboUtil.getSignInstId();

        //修改请求Header
        ChangeDubboUtil.changeHeader(dubboRequest.getHeader(), DubboServiceEnum.M00MO029.getTxnNum());

        //调用Dubbo，获取结果集和相应对象。
        DubboService dubboService = new DubboServiceImpl();
        Result<ManageCashierAccountResponse> result = (Result<ManageCashierAccountResponse>) dubboService.callDubboService(DubboServiceEnum.M00MO029.getServerName(), DubboServiceEnum.M00MO029.getMethodName(), dubboRequest);
        if (result != null && result.isSuccess()) {
            //调用成功
            BizResponse<ManageCashierAccountResponse> bizResponse = new BizResponse<>();
            ChangeDubboUtil.setBizResponse(bizResponse, AppParamConstant.SYS_ID, result);
            return bizResponse;
        } else {
            rglog.error("【商户服务】调用【{}】服务异常！", DubboServiceEnum.M00MO029.getDescription());
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        }
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        ManageCashierAccountResponse modelRespose = (ManageCashierAccountResponse) bizResponse.getResult();
        ConvertUtil.convertInput(modelRespose);
    }
}
